﻿using KinonekoSoftware.CodariaDB.Documents.DataParts.Modules;

namespace KinonekoSoftware.Studio.Models.Modules
{
    public sealed class ColorElementPresenter : ElementPresenter<Element_Color>
    {
        protected override void OnInitialize(Element_Color target)
        {
            RaiseUpdated(nameof(Metadata));
            RaiseUpdated(nameof(Value));
            base.OnInitialize(target);
        }

        /// <summary>
        /// 获得当前内容块的标题。
        /// </summary>
        /// <returns>返回当前内容块的标题。</returns>
        public override string GetTitle() => string.IsNullOrEmpty(Title) ? CodariaSR.Module_Block_Color : Title;
        public override bool IsMultipleVariants() => false;
        public override IEnumerable<Variant> GetVariants(string documentID) => null;
        public override Variant GetVariant(string documentID)
        {
            return new StringVariant
            {
                Id       = documentID,
                Metadata = Metadata,
                Value    = Value,
                Type     = VariantType.Color,
            };
        }

        public string Metadata
        {
            get => AssociatedObject?.Metadata;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }


                RaiseMetadataChanged(AssociatedObject.Metadata, value);
                AssociatedObject.Metadata = value;
                RaiseUpdated();
            }
        }


        public string Fallback
        {
            get => AssociatedObject?.Fallback;
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Fallback = value;
                RaiseUpdated();
            }
        }

        public string Value
        {
            get
            {
                if (AssociatedObject is null)
                {
                    return "#007ACC";
                }

                if (string.IsNullOrEmpty(AssociatedObject.Value))
                {
                    return AssociatedObject.Fallback;
                }

                return AssociatedObject.Value;
            }
            set
            {
                if (AssociatedObject is null)
                {
                    return;
                }

                AssociatedObject.Value = value;
                RaiseUpdated();
                RaisePropertyUpdated(Metadata);
            }
        }
    }
}